package com.sky.service.impl;



import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl  implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    //添加购物车
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart =new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //获取用户id
        Long userid = BaseContext.getCurrentId();
        shoppingCart.setUserId(userid);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //判断当前添加的商品是否已经存在购物车中
        if (list != null && list.size() > 0) {
            //如果存在则只需要数量加1
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            //将修改的数据存入数据库
            shoppingCartMapper.updatenubemByid(cart);
        } else {
            //不存在，则需要插入一条购物车数据
            //判断本次添加的是套餐还是菜品
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null)
            {
                Dish dish = dishMapper.seletById(dishId);
                //封装数据进入购物车对象
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());//金额

            }else {
                //则是套餐
                Long setmealId = shoppingCart.getSetmealId();
                Setmeal setmeal = setmealMapper.selectById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

            }

            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    @Override
    public List<ShoppingCart> list() {
        Long userid = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userid).build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    @Override
    public void deleteShoppingCart() {
        //获取用户id
        Long userid = BaseContext.getCurrentId();
        shoppingCartMapper.deleteAllbyid(userid);
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && list.size() > 0)
        {
            list.forEach(
                    shoppingCart1 -> {
                        //判断数量是否大于1，大于1则数量减1，否则删除该商品
                        if (shoppingCart1.getNumber() > 1)
                        {
                            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
                            shoppingCartMapper.updatenubemByid(shoppingCart1);
                        }else {
                            //判断是菜品还是套餐
                            if (shoppingCart1.getDishId() != null) {
                                shoppingCartMapper.deleteBydishId(shoppingCart1.getDishId());
                            }else{
                                shoppingCartMapper.deleteBydishId(shoppingCart1.getSetmealId());
                            }
                        }
                    }
            );
        }



    }
}
